<template>
  <!-- 页面主体区 -->
  <el-container class="home-container">
    <!-- 页面头部区 -->
    <el-header>
      <div>
        <div>
          <img src="../assets/logo2.jpg" />
        </div>
        <span>电商后台管理系统</span>
       </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <!-- 折叠按钮 -->
        <div class="toggele-button" @click="changeCollaspe">|||</div>
        <!-- 菜单 -->
        <el-menu
          background-color="lightslategray"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="aPath">
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id.toString()"
            v-for="item in menuList"
            :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="chooseActivePath('/'+subItem.path)">

              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data: function () {
    return {
      aPath: '',
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false
    }
  },
  // 调用 vue 的钩子函数created 在挂载完对象之后执行
  created () {
    // 调用获取菜单函数
    this.getMenuList()
    // 页面刷新，从本地将 apath 取出来赋值给 aPath
    this.aPath = window.sessionStorage.getItem('aPath')
  },
  methods: {
    // 设置 菜单高亮
    chooseActivePath (aPath) {
      // 将这个index 值 赋值给 default-active
      this.aPath = aPath
      // 将 index 值保存到本地
      window.sessionStorage.setItem('aPath', aPath)
    },
    logout () {
      // 调用引入的 弹框方法
      this.$messageBox.confirm('确认退出？', '系统提示', {
        confirmButtonText: '确定',
        callback: (action) => {
          if (action === 'confirm') {
            // 清空token
            window.sessionStorage.clear()
            // 跳转到 login 页面
            this.$router.push('/login')
          }
        }
      })
    },
    // 获取菜单列表数据
    async getMenuList () {
      // 发送请求获取左侧菜单数据
      const { data: res } = await this.$http.get('menus')
      // 如果获取数据失败
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 给 data 中的menuLsit 赋值
      this.menuList = res.data
    },
    // 修改菜单折叠状态
    changeCollaspe () {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>
<style lang="less" scoped>
.el-header {
  display: flex;
  justify-content: space-between;
  color: #fff;
  padding-left: 10px;
  align-items: center;
  font-size: 20px;
  background-color: lightslategray;
  div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
    div {
      height: 60px;
      border-radius: 50%;
      overflow: hidden;
      img {
        height: 100%;
      }
    }
  }
}
.el-aside {
  background-color: lightslategray;
  .el-menu{
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.home-container {
  height: 100%;
}
.iconfont{
  margin-right: 10px;
}
.toggele-button{
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
